package com.sltx.util;

import com.alibaba.druid.proxy.jdbc.ClobProxyImpl;

import java.io.BufferedReader;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataFormat {
	/**
	 * TODO<处理金额展示>
	 * @param monery
	 * @return
	 * @throw
	 * @return String
	 */
	public static String moneyFormat(Object monery) {
		DecimalFormat df = new DecimalFormat("0.00");
		String value = "0";
		if(!"0".equals(monery.toString()) && !"-".equals(monery.toString())){
			value = df.format(monery);
		}
		return value;
	}
	
	/**
	 * TODO<处理地区分布 地区名称>
	 * @param disName
	 * @return
	 * @throw
	 * @return String
	 */
	public static String districtSplit(String disName) {
		if (disName.contains("黑龙江") || disName.contains("内蒙古")) {
			return disName.substring(0, 3);
		} else if ((disName.endsWith("省") || disName.endsWith("市") || disName
				.endsWith("自治区") || disName.endsWith("行政区"))
				&& (!disName.contains("黑龙江") && !disName.contains("内蒙古"))) {
			return disName.substring(0, 2);
		} else {
			return disName;
		}
	}
	
	/**
	 * TODO<过滤掉所有数据都为0的>
	 * @param data
	 * @return
	 * @throw
	 * @return boolean
	 */
	public static boolean filterDevoteData(List<Object> data){
		boolean flag = false;
		for(int i = 0; i < data.size(); i++){
			if(!"0".equals(data.get(i).toString())){
				flag = true;
			}
		}
		return flag;
	}
	
	/**
	 * TODO<处理金额展示--带千位分隔符>
	 * @param monery
	 * @return
	 * @throw
	 * @return String
	 */
	public static String moneySplitFormat(Object monery) {
		DecimalFormat df = new DecimalFormat("#,###.00");
		String value = "0";
		if(!"0".equals(monery.toString())){
			value = df.format(monery);
		}
		return value;
	}
	
	public static String moneySplitThreeFormat(Object monery) {
		DecimalFormat df = new DecimalFormat("#,##0.000");
		String value = "0";
		if(!"0".equals(monery.toString())){
			value = df.format(monery);
		}
		return value;
	}
	
	public static Integer strToInteger(Object param){
		return Integer.parseInt(param.toString());
	}
	
	public static Double strToDouble(Object param){
		return Double.parseDouble(moneyFormat(param));
	}
	
	public static Double charToDouble(String param){
		BigDecimal b = new BigDecimal(param);
		return Double.parseDouble(moneyFormat(b));
	}
	
	public static boolean validNum(String str){
	   Pattern pattern = Pattern.compile("[0-9]*"); 
	   Matcher isNum = pattern.matcher(str);
	   if(!isNum.matches()){
	       return false; 
	   } 
	   return true; 
	}
	
	public static String dealString(String str){
		if(str.contains("\"")){
			str = str.replace("\"", "");
		}
		if(str.contains("[")){
			str = str.replace("[", "");
		}
		if(str.contains("]")){
			str = str.replace("]", "");
		}
		return str;
	}
	
	/**
	 * TODO<处理数量展示>
	 * 
	 * @param monery
	 * @return
	 * @throw
	 * @return String
	 */
	public static String amountFormat(Object monery) {
		DecimalFormat df = new DecimalFormat("#,###");
		String value = "0";
		if(!"0".equals(monery.toString())){
			value = df.format(monery);
		}
		return value;
	}
	
	 public static final String convertClobToString(ClobProxyImpl clob) throws Exception {
        StringBuilder sb = new StringBuilder();
        BufferedReader bf = new BufferedReader(clob.getCharacterStream());
        String line = bf.readLine();
        while (line != null){
            sb.append(line);
            line = bf.readLine();
        }
        return sb.toString();
	}
	 
	 public static void handleColbColumn(PageData pd,String colName){
		if (null != pd.get(colName) && !"".equals(pd.get(colName).toString())) {
			Object obj = pd.get(colName);
			if(obj.getClass() == ClobProxyImpl.class){
				ClobProxyImpl clob  = (ClobProxyImpl) pd.get(colName);
				String yqtzqy = null;
	            try {
	            	yqtzqy = convertClobToString(clob);
	            } catch (Exception ex) {
	            	System.out.println(ex.toString());
	            }
	            pd.put(colName, yqtzqy);
			}else{
				pd.put(colName, obj);
			}
	    }else{
	    	pd.put(colName, "");
	    }
    }

}
